<?php

namespace app\parking\controller\admin;

use think\admin\Controller;
use think\Request;
use app\parking\model\ParkingVisitor as VisitorModel;
use app\parking\model\ParkingCompany as CompanyModel;
use app\parking\model\ParkingParking as ParkingModel;

class Visitor extends Controller
{
    /**
     * 访客管理列表页面
     */
    public function index(Request $request)
    {
        $this->title = '访客管理';
        return $this->fetch();
    }

    /**
     * 访客管理列表
     */
    public function get(Request $request)
    {
        $query = VisitorModel::alias('v')
            ->join('parking_company c', 'v.company_id = c.id')
            ->field('v.*, c.name as company_name')
            ->order('v.id desc');

        // 搜索过滤
        if ($plateNumber = $request->get('plate_number')) {
            $query->where('v.plate_number', 'like', "%{$plateNumber}%");
        }

        $data = $query->paginate(10)->toArray();
        return json([
            'code' => 0,
            'msg' => '',
            'count' => $data['total'],
            'data' => $data['data']
        ]);
    }

    /**
     * 添加访客
     */
    public function add()
    {
        $this->assign('companies', CompanyModel::where('status', 1)->select());
        return $this->fetch('form');
    }

    /**
     * 保存访客
     */
    public function save(Request $request)
    {
        $data = $request->post();
        $plateNumber = $data['plate_number'];
        $freeHours = $data['free_hours'];

        $parkingRecord = ParkingModel::where('plate_number', $plateNumber)
            ->where('is_exited', 1)
            ->order('entry_time', 'desc')
            ->find();

        if (!$parkingRecord) {
            return $this->error('未找到当前车辆停车记录');
        }

        if ($parkingRecord->free_hours == 0) {
            $parkingRecord->free_hours = $freeHours;
            $parkingRecord->save();
            $data['parking_record_id'] = $parkingRecord->id;
            VisitorModel::create($data);
            return $this->success('访客记录添加成功');
        } else {
            return json([
                'code' => 2,
                'info' => "查询到当前车辆已有{$parkingRecord->free_hours}小时免费停车时长，是否继续添加",
                'data' => [
                    'free_hours' => $parkingRecord->free_hours,
                    'parking_record_id' => $parkingRecord->id,
                    'additional_data' => $data
                ]
            ]);
        }
    }

    /**
     * 确认增加免费停车时长
     */
    public function confirm(Request $request)
    {
        $data = $request->post();
        $parkingRecordId = $data['parking_record_id'];
        $additionalHours = $data['free_hours'];
        $additionalData = $data['additional_data'];

        $parkingRecord = ParkingModel::find($parkingRecordId);
        if (!$parkingRecord) {
            return $this->error('未找到停车记录');
        }

        $parkingRecord->free_hours += $additionalHours;
        $parkingRecord->save();

        $additionalData['parking_record_id'] = $parkingRecordId;
        VisitorModel::create($additionalData);

        return $this->success('免费停车时长添加成功');
    }

    /**
     * 编辑访客
     */
    public function edit($id)
    {
        $this->assign('vo', VisitorModel::find($id));
        $this->assign('companies', CompanyModel::where('status', 1)->select());
        return $this->fetch('form');
    }

    /**
     * 删除访客
     */
    public function delete($id)
    {
        VisitorModel::destroy($id);
        $this->success('访客删除成功');
    }
}
